#include <cstdio>

#include <cmemory.h>
#include <scrptrun.h>
#include <unicode/ubidi.h>

UChar testChars[] = {
    0x0020, 0x0946, 0x0939, 0x093F, 0x0928, 0x094D, 0x0926, 0x0940, 0x0020,
    0x0627, 0x0644, 0x0639, 0x0631, 0x0628, 0x064A, 0x0629, 0x0020,
    0x0420, 0x0443, 0x0441, 0x0441, 0x043A, 0x0438, 0x0439, 0x0020,
    'E', 'n', 'g', 'l', 'i', 's', 'h',  0x0020,
    0x6F22, 0x5B75, 0x3068, 0x3072, 0x3089, 0x304C, 0x306A, 0x3068,
    0x30AB, 0x30BF, 0x30AB, 0x30CA,
    0xD801, 0xDC00, 0xD801, 0xDC01, 0xD801, 0xDC02, 0xD801, 0xDC03
};

int main()
{
    UBiDi* bidi = ubidi_open();
    UBiDiLevel bidiReq = UBIDI_DEFAULT_LTR;
    if (bidi) {
        UErrorCode status = U_ZERO_ERROR;
        ubidi_setPara(bidi, testChars, UPRV_LENGTHOF(testChars), bidiReq, NULL, &status);
        if (U_SUCCESS(status)) {
            ubidi_getParaLevel(bidi);
            auto rc = ubidi_countRuns(bidi, &status);

            for (size_t i = 0; i < size_t(rc); ++i) {
                int32_t startRun = -1;
                int32_t lengthRun = -1;
                UBiDiDirection runDir = ubidi_getVisualRun(bidi, i, &startRun, &lengthRun);
                bool isRTL = (runDir == UBIDI_RTL);
                printf("Processing Bidi Run = %lu -- run-start = %d, run-len = %d, isRTL = %d\n",
                                                    i, startRun, lengthRun, isRTL);

                icu::ScriptRun scriptRun(testChars, startRun, lengthRun);
                while (scriptRun.next()) {
                    int32_t     start = scriptRun.getScriptStart();
                    int32_t     end   = scriptRun.getScriptEnd();
                    UScriptCode code  = scriptRun.getScriptCode();

                    printf("Script '%s' from %d to %d.\n", uscript_getName(code), start, end);
                }
            }
        }
    }
}
